package com.springboot.template.controller;

import com.alibaba.fastjson2.JSONObject;
import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.springboot.template.domain.SecurityUser;
import com.springboot.template.service.ISecurityKeyInfoService;
import com.springboot.template.service.ISecurityUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * 用户Controller
 *
 * @author ruoyi
 * @date 2024-05-30
 */
@RestController
@RequestMapping("/user")
@CrossOrigin
public class SecurityUserController
{
    @Autowired
    private ISecurityUserService securityUserService;

    /**
     * 查询用户列表
     */
    @GetMapping("/list")
    public List<SecurityUser> list(SecurityUser securityUser)
    {
        List<SecurityUser> list = securityUserService.selectSecurityUserList(securityUser);
        return list;
    }

    /**
     * 获取用户详细信息
     */
    @GetMapping(value = "/{id}")
    public SecurityUser getInfo(@PathVariable("id") Long id)
    {
        return securityUserService.selectSecurityUserById(id);
    }

    /**
     * 新增用户
     */
    @PostMapping
    public Integer add(@RequestBody SecurityUser securityUser)
    {
        return securityUserService.insertSecurityUser(securityUser);
    }

    /**
     * 修改用户
     */
    @PutMapping
    public Integer edit(@RequestBody SecurityUser securityUser)
    {
        return securityUserService.updateSecurityUser(securityUser);
    }

    /**
     * 删除用户
     */
    @DeleteMapping("/{ids}")
    public Integer remove(@PathVariable Long[] ids)
    {
        return securityUserService.deleteSecurityUserByIds(ids);
    }

    /**
     * 验证用户token
     * @param securityUser
     * @return
     */
    @GetMapping("/verify")
    public Object verifySecurityUser(SecurityUser securityUser) {
        JSONObject jsonObject = new JSONObject();
        int result = securityUserService.verifySecurityUser(securityUser);
        if(result > 0) {
            jsonObject.put("code", 200);
            jsonObject.put("msg", "校验成功");
        }else {
            jsonObject.put("code", 500);
            jsonObject.put("msg", "校验失败");
        }
        return jsonObject;
    }

    /**
     * 生成用户token
     * @param securityUser
     * @return
     */
    @PostMapping("/generate")
    public Object generateSecurityUser(@RequestBody SecurityUser securityUser) {
        JSONObject jsonObject = new JSONObject();
        String token = securityUserService.generateSecurityUser(securityUser);
        if(StringUtils.isNotEmpty(token)) {
            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("token",token);
            jsonObject.put("data",jsonObject1);
            jsonObject.put("code", 200);
            jsonObject.put("msg", "请求成功");
        }else {
            jsonObject.put("code", 500);
            jsonObject.put("msg", "请求失败");
        }
        return jsonObject;
    }

    /**
     * 生成用户token--加密版
     * @param body 数据体
     * @return
     */
    @PostMapping("/generate/v1")
    public Object generateSecurityUserV1(@RequestBody Map<String, String> body) throws Exception {
        JSONObject jsonObject = new JSONObject();
        securityUserService.generateSecurityUserV1(body.get("encryptString"), body.get("channelId"));
//        JSONObject jsonObject1 = new JSONObject();
//        String token = securityUserService.generateSecurityUser(securityUser);
//        if(StringUtils.isNotEmpty(token)) {
//            JSONObject jsonObject1 = new JSONObject();
//            jsonObject1.put("token",token);
//            jsonObject.put("data",jsonObject1);
//            jsonObject.put("code", 200);
//            jsonObject.put("msg", "请求成功");
//        }else {
//            jsonObject.put("code", 500);
//            jsonObject.put("msg", "请求失败");
//        }
        return jsonObject;
    }
}
